import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/core/constans/dict_data_info.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/basic_information/major_hazard_information_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/filter/filter_bean.dart';
import 'package:enterprise_v6_app/app/data/entities/application/filter/filter_entities_bean.dart';
import 'package:enterprise_v6_app/app/data/entities/application/get_dict_all_entity.dart';

///重大危险源页面
class MajorHazardBaseController extends GetxController {
  /// 列表刷新key
  final GlobalKey<CustomEasyRefreshListState> globalKey = GlobalKey();

  /// 筛选右侧栏控制Key
  final popRightScaffoldKey = GlobalKey<ScaffoldState>();

  /// 筛选列表
  List<FilterBean> filterList = [];

  /// 搜索条件
  String? search;

  /// 选中的危险源等级
  int? hazardRank;

  /// 选中的危险源分类
  String? hazardType;

  // 获取重大危险源数据
  Future<List<MajorHazardInformationEntity>> getListData(int pageNum, int pageSize) async {
    // 获取列表接口
    final res = await HttpUtil().generateGetRequest<List<MajorHazardInformationEntity>>(
      Interface.getMajorHazardByPageAPP,
      queryParameters: {
        'pageNum': pageNum,
        'pageSize': pageSize,
        'hazardParkName': search,
        'hazardRank': hazardRank,
        'hazardType': hazardType,
      },
    ).execute();
    List<MajorHazardInformationEntity>? data;
    res.onSuccess((result) {
      if (result.data != null) {
        data = result.data!;
      }
    });
    return data ?? [];
  }

  @override
  void onInit() {
    super.onInit();
    initData();
  }

  /// 初始化数据
  Future<void> initData() async {
    // 获取后端危险源类型字典
    final List<GetDictAllEntity> dictTypeList = await DictDataInfo.getDictionaryDataByTree(dictionaryType: 'zdwxylx_RYwzEis0');

    final List<FilterEntitiesBean> typeList = [];

    // 添加类型筛选条件
    for (var item in dictTypeList) {
      typeList.add(FilterEntitiesBean(name: item.dictionaryLabel!, value: item.id!));
    }

    final List<FilterEntitiesBean> rankList = [];

    // 添加等级筛选条件
    rankList.add(FilterEntitiesBean(name: '一级', value: 1));
    rankList.add(FilterEntitiesBean(name: '二级', value: 2));
    rankList.add(FilterEntitiesBean(name: '三级', value: 3));
    rankList.add(FilterEntitiesBean(name: '四级', value: 4));

    //筛选控制bean
    final FilterBean filterBean = FilterBean(title: '等级', filterList: rankList);
    final FilterBean filterBean3 = FilterBean(title: '分类', filterList: typeList);
    filterList.add(filterBean);
    filterList.add(filterBean3);
  }

  /// 筛选确认
  void screenConfirm(List<FilterBean> value) {
    filterList = value;
    hazardRank = null;
    hazardType = null;
    for (var i in value) {
      if (i.chosenValue.isEmpty) {
        continue;
      }
      if (i.title == '等级') {
        hazardRank = i.chosenValue[0].value as int;
      }
      if (i.title == '分类') {
        hazardType = i.chosenValue[0].value as String;
      }
    }
    update();
    refreshPage();
  }

  ///筛选按钮
  void onFilterButtonPressed() {
    popRightScaffoldKey.currentState?.openEndDrawer();
  }

  ///页面刷新
  void refreshPage() {
    globalKey.currentState?.onRefresh();
  }
}
